﻿#include <QFileDialog>
#include "GlobalData.h"
#include "PageSampleProperty.h"
#include "ui_PageSampleProperty.h"
#include "../DatabaseMgmt/SampleProperty.h"


CPageSampleProperty::CPageSampleProperty(QWidget* parent /*= nullptr*/, Qt::WindowFlags f /*= 0*/)
	: QWidget(parent, f)
{
	init();
}

CPageSampleProperty::~CPageSampleProperty()
{

}

void CPageSampleProperty::openTestReport()
{
	uint32_t nTestReportId = CGlobalData::getInstance()->getTestReportUid();

	if (nTestReportId == 0)
	{
		return;
	}
	CSampleProperty record;

	SampleProperty property;
	record.queryByReportUid(nTestReportId, property);

	// 样品包装
	m_ui->lePackDesc->setText(property.pack_describe);
	QPixmap pixmap_pack(property.pack_image);
	m_ui->lblPicturePack->setPixmap(pixmap_pack);

	// 样品外观
	m_ui->leExteriorDesc->setText(property.exterior_describe);
	QPixmap pixmap_exterior(property.exterior_image);
	m_ui->lblPictureExterior->setPixmap(pixmap_exterior);
}

void CPageSampleProperty::savePage()
{
	slotSave();
}

void CPageSampleProperty::clear()
{
	m_ui->lePackDesc->clear();

	m_ui->leExteriorDesc->clear();
}

void CPageSampleProperty::init()
{
	m_ui = new Ui::PageSampleProperty();

	m_ui->setupUi(this);

	connect(m_ui->btnImagePack, SIGNAL(clicked()), this, SLOT(slotSelectPackPhoto()));
	connect(m_ui->btnImageExterior, SIGNAL(clicked()), this, SLOT(slotSelectExteriorPhoto()));
	connect(m_ui->btnSave, SIGNAL(clicked()), this, SLOT(slotSave()));
}

void CPageSampleProperty::slotSelectPackPhoto()
{
	uint32_t nTestReportId = CGlobalData::getInstance()->getTestReportUid();

	if (nTestReportId == 0)
	{
		return;
	}

	QString image = QFileDialog::getOpenFileName(this, QStringLiteral("图片选择"),
		QDir::home().absolutePath(), "Images (*.png *.bmp *.jpg)");

	if (image.isEmpty())
	{
		return;
	}

	QPixmap pixmap(image);

	m_ui->lblPicturePack->setPixmap(pixmap);

	bool ret = pixmap.save(generatePackImagePath(), "PNG");
	qInfo() << QStringLiteral("保存包装图片：") << ret;
}

void CPageSampleProperty::slotSelectExteriorPhoto()
{
	uint32_t nTestReportId = CGlobalData::getInstance()->getTestReportUid();

	if (nTestReportId == 0)
	{
		return;
	}

	QString image = QFileDialog::getOpenFileName(this, QStringLiteral("图片选择"),
		QDir::home().absolutePath(), "Images (*.png *.bmp *.jpg)");
	
	if (image.isEmpty())
	{
		return;
	}

	QPixmap pixmap(image);

	m_ui->lblPictureExterior->setPixmap(pixmap);

	bool ret = pixmap.save(generateExteriorImagePath(), "PNG");
	qInfo() << QStringLiteral("保存外观图片：") << ret;
}

void CPageSampleProperty::slotSave()
{
	uint32_t nTestReportId = CGlobalData::getInstance()->getTestReportUid();

	if (nTestReportId == 0)
	{
		return;
	}
	SampleProperty property;

	property.report_uid = nTestReportId;
	property.pack_describe = m_ui->lePackDesc->text().trimmed();
	property.pack_image = generatePackImagePath();
	property.exterior_describe = m_ui->leExteriorDesc->text().trimmed();
	property.exterior_image = generateExteriorImagePath();

	CSampleProperty record;
	if (record.insertRow(property) != CONST_ERR_SUCCESS)
	{
		qInfo() << QStringLiteral("保存样品性状失败，报告ID：") << nTestReportId;
	}
}

QString CPageSampleProperty::generatePackImagePath()
{
	QString strText;
	uint32_t nTestReportId = CGlobalData::getInstance()->getTestReportUid();

	if (nTestReportId == 0)
	{
		return strText;
	}
	strText = "../data/images/test_report_" + QString::number(nTestReportId) + "_pack.png";
	return strText;
}

QString CPageSampleProperty::generateExteriorImagePath()
{
	QString strText;
	uint32_t nTestReportId = CGlobalData::getInstance()->getTestReportUid();

	if (nTestReportId == 0)
	{
		return strText;
	}

	strText = "../data/images/test_report_" + QString::number(nTestReportId) + "_exterior.png";

	return strText;
}

